package com.example.pei.ithinklearnt.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * 权限根据类
 * Created by Pei on 12/3/2017.
 */

public class PermisionUtil {
    public static void achieveAllPermisions(Activity activity,int requestCode){
        if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED ||
                ContextCompat.checkSelfPermission(activity, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(activity,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                            Manifest.permission.WRITE_EXTERNAL_STORAGE,
                            Manifest.permission.CAMERA,
                            Manifest.permission.CALL_PHONE,
                            Manifest.permission.READ_PHONE_STATE}, requestCode);
        }
    }

    //检查权限，如果拥有读写权限，则返回true，否则开始申请读写权限并且返回false
    public static boolean checkWritePermission(Activity activity,int requestCode){
        if (ContextCompat.checkSelfPermission(activity, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);
            return false;
        } else {
            //有权限
           return true;
        }
    }



    public static boolean hasReadPhoneStatePermission(Context context) {
        return EasyPermissions.hasPermissions(context, Manifest.permission.READ_PHONE_STATE);
    }

    public static boolean hasCoarseLocationPermission(Context context) {
        return EasyPermissions.hasPermissions(context, Manifest.permission.ACCESS_COARSE_LOCATION);
    }

    public static boolean hasReadRecordAudioPermission(Context context){
        return EasyPermissions.hasPermissions(context, Manifest.permission.RECORD_AUDIO);
    }

//    private boolean hasWriteExtralStoragePermission(Context context) {
//        return EasyPermissions.hasPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
//    }





}
